package com.cls.business.mapper;

import com.cls.business.entity.DeclareTopic;
import com.cls.business.entity.Review;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.cls.business.vo.ExpertReviewResultVO;
import com.cls.business.vo.ReviewVO;
import com.cls.system.entity.Expert;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * Mapper
 *
 * @author wandering
 * @date 2020-11-18 11:58:54
 */
@Repository
public interface ReviewMapper extends BaseMapper<Review> {
    @Select("SELECT * " +
            "FROM biz_review WHERE declare_id=#{declareId} and expert_id=#{expertId} and is_delete=0 limit 1 ")
    Review findByDeclareIdAndExpertId(@Param("declareId") Long declareId, @Param("expertId") Long expertId);


    /**
     * 魏起成
     * 根据declareId进行查询数据
     *
     * @param declareId
     * @return
     */
    List<Map<String, Object>> selectResultByDeclareId(@Param("declareId") Long declareId,@Param("expertList") List<Expert> expertList,@Param("planId")Long planId);

    /**
     * 根据declareId进行查询专家
     *
     * @param declareId
     * @return
     */
    @Select("SELECT expert.* from biz_review review,biz_expert expert " +
            " WHERE " +
            " review.expert_id=expert.expert_id " +
            " AND " +
            " review.is_delete=0 " +
            " AND " +
            " expert.is_delete=0 " +
            " AND " +
            " review.declare_id=#{declareId} and review.plan_id=#{planId} order by expert.expert_id ")
    List<Expert> selectExpertByDeclareId(@Param("declareId") Long declareId,@Param("planId")Long planId);

    /**
     * 根据declareId进行查询
     * @param declareId
     * @return
     */
    @Select("SELECT * FROM biz_review WHERE declare_id=#{declareId} AND is_delete=0")
    List<Review> selectByDeclareId(Long declareId);
    /**
     * 根据declareId和专家id查询进行查询
     * @param declareId
     * @param declareId
     * @return
     */
    @Select("SELECT * FROM biz_review WHERE declare_id=#{declareId} AND expert_id=#{expertId} AND is_delete=0 limit 1")
    Review selectByDeclareIdAndExpertId(@Param("declareId")Long declareId,@Param("expertId") Long expertId);


    @Select("SELECT topic.*, review.is_submit as isReviewForExpert FROM biz_review review " +
            "LEFT JOIN biz_declare_topic topic on review.declare_id = topic.declare_id " +
            "WHERE review.plan_id=#{planId} AND review.expert_id=#{expertId} AND review.is_delete=0 order by topic.code ")
    List<DeclareTopic> selectDeclareTopicByPlanIdAndExpertId(@Param("planId") Long planId, @Param("expertId") Long expertId);


    List<DeclareTopic> selectDeclareTopicView(Map param);

    @Select("SELECT * FROM biz_review WHERE plan_id=#{planId} AND expert_id=#{expertId} AND is_delete=0 ")
    List<Review> selectByPlanIdAndExpertId(@Param("planId") Long planId, @Param("expertId") Long expertId);

    @Delete("DELETE FROM biz_review WHERE plan_id=#{planId} AND is_delete=0 ")
    int deleteByPlanId(@Param("planId") Long planId);

    @Select("SELECT * FROM biz_review WHERE group_id=#{groupId} AND is_delete=0 ")
    List<Review> selectByGroupId(@Param("groupId") Long groupId);



    @Select("    select a.expert_id,a.declare_id,topic.name,info.company, topic.leader_name, topic.code, ind.description, a.score,a.totalScore,a.opinion,a.indicator_id  " +
            "    from  (select   CONCAT(ind_det.indicator_type,'-',rw_det.score) as score, ind_det.indicator_id as indicator_id, rw.expert_id , " +
            " rw.declare_id, rw.score as totalScore, rw.opinion as opinion  " +
            "             from biz_review_detail rw_det  " +
            "             left join biz_indicator_detail ind_det on ind_det.indicator_detail_id = rw_det.indicator_detail_id  " +
            "             left join biz_review rw  on rw.review_id = rw_det.review_id  " +
            "             where rw.plan_id = #{planId} AND rw_det.is_delete = 0 ) a  " +
            "  LEFT JOIN biz_indicator ind on a.indicator_id = ind.indicator_id and ind.standard_id = #{standardId} " +
            "  LEFT JOIN biz_declare_topic topic on a.declare_id = topic.declare_id " +
            "  LEFT JOIN biz_applicant_info info on info.applicant_id = topic.applicant_id " +
            "  order by a.expert_id,topic.topic_code,a.indicator_id")
    List<ExpertReviewResultVO> selectReviewByPlanIdAndStandardId(@Param("planId") Long planId, @Param("standardId") Long standardId);
}
